/**-------------------------------------------------------------------------------------------
** Created by:          qinyx
** Last modified Date:  2014-02-28
** Last Version:        V1.00
** Descriptions:        STM32F407嵌入式实验箱
**
**
**-------------------------------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "StepMotor.h"
#include "delay.h"

/******************************************************************************************
*函数名称：void RelayGpio_Init(void)
*
*入口参数：无
*
*出口参数：无
*
*功能说明：LED初始化
//MOTOR_A      PF3
//MOTOR_B      PF4
//MOTOR_C      PE5
//MOTOR_D      PE3
*******************************************************************************************/
void StepMotorGpio_Init(void)
{    
    GPIO_InitTypeDef  GPIO_InitStructure;
    
    //MOTOR_A      PF3
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOF, &GPIO_InitStructure);
    
    //MOTOR_B      PF4
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOF, &GPIO_InitStructure);
    
    //MOTOR_C      PE5
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    
    //MOTOR_D      PE3
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    
    MOTOR_A_H;
    MOTOR_B_H;
    MOTOR_C_H;
    MOTOR_D_H;
}

/**********************************************************************************************************
函数名称：步进电机测试函数
输入参数：模式，0为正转，1为反转
输出参数：无
**********************************************************************************************************/
void StepMotorTest(unsigned char mode)
{
    unsigned int i = 0;
    
    if (mode == 0)
    {
        for ( i =0; i < 200; i++)
        {
            MOTOR_A_L;
            delay_ms(5);
            MOTOR_A_H;
            
            MOTOR_B_L;
            delay_ms(5);
            MOTOR_B_H;
            
            MOTOR_C_L;
            delay_ms(5);
            MOTOR_C_H;
            
            MOTOR_D_L;
            delay_ms(5);
            MOTOR_D_H; 
        }
    }
    
    else
    {
        for ( i =0; i < 200; i++)
        {
            MOTOR_D_L;
            delay_ms(5);
            MOTOR_D_H;
            
            MOTOR_C_L;
            delay_ms(5);
            MOTOR_C_H;
            
            MOTOR_B_L;
            delay_ms(5);
            MOTOR_B_H;
            
            MOTOR_A_L;
            delay_ms(5);
            MOTOR_A_H; 
        }
    }
    MOTOR_A_L;
    MOTOR_B_L;
    MOTOR_C_L;
    MOTOR_D_L;
}
